<?php
declare (strict_types=1);

namespace app\admin\controller;

use addons\BuilderPlus\components\Button;
use addons\BuilderPlus\components\Column;
use addons\BuilderPlus\components\Search;
use addons\BuilderPlus\facade\Table;
use app\common\model\ProductType;
use app\common\tool\Result;
use think\Response;

class Product extends Base
{
    public function getOptions(): array
    {
        return [
            'pk' => 'id',
            'model' => \app\common\model\Product::class,
            'validate' => \app\admin\validate\Product::class,
            'with' => ['type', 'cate1', 'cate2', 'supplier'],
            'just' => false,
            'fast' => [
                'add' => 'product/edit',
                'edit' => 'product/edit',
                'save' => '_save'
            ],
            'tape' => [
                ['field' => 'icon', 'title' => '图片', 'type' => 'icon'],
                ['field' => 'price', 'title' => '市场价'],
                ['field' => 'vip_price', 'title' => '会员价'],
                ['field' => 'stock', 'title' => '库存', 'type' => 'stock']
            ]
        ];
    }

    public function index(): string
    {
        return Table::init($this)
            ->addColumns([
                ['id', 'ID', fn(Column $col) => $col->width(80)],
                ['title', '商品名称'],
                ['supplier_id', '供应商', fn(Column $col) => $col->templet('{{d.supplier?d.supplier.title:"-"}}')->width(100)],
                ['icon', '主图', fn(Column $col) => $col->lazyImage()->width(100)],
                ['type_id', '所属专区', fn(Column $col) => $col->templet('{{d.type?d.type.title:"-"}}')->width(100)],
                //['cate_1', '所属分类', fn(Column $col) => $col->templet('{{d.cate1?d.cate1.title:""}}-{{d.cate2?d.cate2.title:""}}')->width(150)],
                ['price', '市场价', fn(Column $col) => $col->width(100)],
                ['vip_price', '会员价', fn(Column $col) => $col->width(100)],
                ['stock', '库存', fn(Column $col) => $col->width(100)],
                ['sales', '销量', fn(Column $col) => $col->width(100)],
                ['virtual_sales', '虚拟销量', fn(Column $col) => $col->width(100)],
                ['sort', '排序', fn(Column $col) => $col->fastSort()],
                ['create_time', '添加时间', fn(Column $col) => $col->width(150)],
                ['update_time', '更新时间', fn(Column $col) => $col->width(150)],
                ['pick', '推荐', fn(Column $col) => $col->switch('开启|关闭', '1|0')->fixed('right')],
                ['status', '状态', fn(Column $col) => $col->switch('开启|关闭', '1|0')->fixed('right')]
            ])
            ->addTopButtons([
                'add' => fn(Button $btn) => $btn->event('newPage'),
                'deleteAll'
            ])
            ->addRightButtons([
                'edit' => fn(Button $btn) => $btn->event('newPage'),
                'delete'
            ])
            ->addSearch([
                ['create_time', '添加时间', fn(Search $search) => $search->type('date')],
                ['title', '商品名称', fn(Search $search) => $search->condition('Like')],
                ['supplier_id', '供应商', fn(Search $search) => $search->type('select')->optionValue(\app\common\model\Supplier::column('title', 'id'))],
                ['type_id', '所属专区', fn(Search $search) => $search->type('select')->optionValue(ProductType::column('title', 'id'))],
                //['cate_1-cate_2', '所属分类', fn(Search $search) => $search->type('linkage')->linkageUrl(url('productCate/getCate')->build())]
            ])
            ->fetch();
    }

    public function getSpecOptions(): Response
    {
        $id = $this->request->get('id');
        $type = ProductType::field('id,title')->select();
        foreach ($type as &$item) {
            switch ($item['id']) {
//                //假设报单区有个赠送积分
//                case 1:
//                    $item['tape'] = array_merge($this->tape, [['field' => 'give', 'title' => '赠送积分']]);
//                    break;
                default:
                    $item['tape'] = $this->getOptions()['tape'];

            }
        }

        return Result::success('success', [
            'type' => $type,
            'info' => $id ? $this->getOptions()['model']::where('id', $id)->with(['productSpec'])->find() : '',
            'specTpl' => \app\common\model\Spec::field('id,title')->order('id', 'desc')->select()
        ]);
    }
}